﻿using System.Linq;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace MvcGlobalisationSupport
{
    public static class GlobalisationHtmlHelperExtensions
    {
        static void AddOtherValues(RouteData routeData, RouteValueDictionary destinationRoute)
        {
            foreach (
                var routeInformation in
                    routeData.Values.Where(routeInformation => routeInformation.Key != GlobalisedRoute.CultureKey))
                destinationRoute.Add(routeInformation.Key, routeInformation.Value);
        }

        public static MvcHtmlString GlobalisedRouteLink(this HtmlHelper htmlHelper, string linkText, string targetCultureName, RouteData routeData)
        {
            var globalisedRouteData = new RouteValueDictionary {{GlobalisedRoute.CultureKey, targetCultureName}};
            AddOtherValues(routeData, globalisedRouteData);
            return htmlHelper.RouteLink(linkText, globalisedRouteData);
        }
    }
}
